using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DataProvider;
using Bussiness.Data;
using Bussiness.Exc;
using Bussiness.Interfaces;

namespace Bussiness
{
    public class CTKTRAProcess:ICTKTRA
    {
        IDBManager idbManager = new DBManager(DataProviderE.SqlServer);

        DtsCTKTRA ICTKTRA.XemCTKTRA()
        {
            DtsCTKTRA dtsCTKTRA = new DtsCTKTRA();
            try
            {
                idbManager.Open();
                dtsCTKTRA.Tables.Add(idbManager.ExecuteDataSet(CommandType.StoredProcedure, "spCTKTRAXem").Tables[0].Copy());
            }
            catch (DatabaseExc e)
            {
                throw new BussinessExc(e.Message, e.ErrCodeUser, e.ErrCodeSQL);
            }
            catch (Exception e)
            {
                throw new BussinessExc("Bussiness. ICTKTRA.XemCTKTRA()" + e.Message);
            }
            finally
            {
                idbManager.Dispose();
            }
            return dtsCTKTRA;
        }
        bool ICTKTRA.UpdateSP(DtsCTKTRA dtsCTKTRA)
        {
            bool success = false;

            try
            {
                IDataParameter[] arrParamInsert = DBManagerFactory.GetParameters(idbManager.ProviderType, 7);
                arrParamInsert[0] = DBManagerFactory.GetParameterSQL("@RETURN_VALUE", SqlDbType.Variant, 0, System.Data.ParameterDirection.ReturnValue, false, ((byte)(0)), ((byte)(0)), "", System.Data.DataRowVersion.Current, null);
                arrParamInsert[1] = DBManagerFactory.GetParameterSQL("@Hocky", SqlDbType.TinyInt, 0, "Hocky");
                arrParamInsert[2] = DBManagerFactory.GetParameterSQL("@Mahs", SqlDbType.NChar, 0, "Mahs");
                arrParamInsert[3] = DBManagerFactory.GetParameterSQL("@Mamon", SqlDbType.NChar, 0, "Mamon");
                arrParamInsert[4] = DBManagerFactory.GetParameterSQL("@Makt", SqlDbType.NChar, 0, "Makt");
                arrParamInsert[5] = DBManagerFactory.GetParameterSQL("@Diem", System.Data.SqlDbType.Decimal, 0, System.Data.ParameterDirection.Input, false, ((byte)(4)), ((byte)(2)), "Diem", System.Data.DataRowVersion.Current, null);
                arrParamInsert[6] = DBManagerFactory.GetParameterSQL("@Malop", System.Data.SqlDbType.NChar, 0, "Malop");

                IDataParameter[] arrParamUpdate = DBManagerFactory.GetParameters(idbManager.ProviderType, 11);
                arrParamUpdate[0] = DBManagerFactory.GetParameterSQL("@RETURN_VALUE", System.Data.SqlDbType.Variant, 0, System.Data.ParameterDirection.ReturnValue, false, ((byte)(0)), ((byte)(0)), "", System.Data.DataRowVersion.Current, null);
                arrParamUpdate[1] = DBManagerFactory.GetParameterSQL("@Hocky", System.Data.SqlDbType.TinyInt, 0, "Hocky");
                arrParamUpdate[2] = DBManagerFactory.GetParameterSQL("@Mahs", System.Data.SqlDbType.NChar, 0, "Mahs");
                arrParamUpdate[3] = DBManagerFactory.GetParameterSQL("@Mamon", System.Data.SqlDbType.NChar, 0, "Mamon");
                arrParamUpdate[4] = DBManagerFactory.GetParameterSQL("@Makt", System.Data.SqlDbType.NChar, 0, "Makt");
                arrParamUpdate[5] = DBManagerFactory.GetParameterSQL("@Diem", System.Data.SqlDbType.Decimal, 0, System.Data.ParameterDirection.Input, false, ((byte)(4)), ((byte)(2)), "Diem", System.Data.DataRowVersion.Current, null);
                arrParamUpdate[6] = DBManagerFactory.GetParameterSQL("@Malop", System.Data.SqlDbType.NChar, 0, "Malop");
                arrParamUpdate[7] = DBManagerFactory.GetParameterSQL("@Original_Hocky", System.Data.SqlDbType.TinyInt, 0, System.Data.ParameterDirection.Input, false, ((byte)(0)), ((byte)(0)), "Hocky", System.Data.DataRowVersion.Original, null);
                arrParamUpdate[8] = DBManagerFactory.GetParameterSQL("@Original_Mahs", System.Data.SqlDbType.NChar, 0, System.Data.ParameterDirection.Input, false, ((byte)(0)), ((byte)(0)), "Mahs", System.Data.DataRowVersion.Original, null);
                arrParamUpdate[9] = DBManagerFactory.GetParameterSQL("@Original_Mamon", System.Data.SqlDbType.NChar, 0, System.Data.ParameterDirection.Input, false, ((byte)(0)), ((byte)(0)), "Mamon", System.Data.DataRowVersion.Original, null);
                arrParamUpdate[10] = DBManagerFactory.GetParameterSQL("@Original_Makt", System.Data.SqlDbType.NChar, 0, System.Data.ParameterDirection.Input, false, ((byte)(0)), ((byte)(0)), "Makt", System.Data.DataRowVersion.Original, null);

                IDataParameter[] arrParamDelete = DBManagerFactory.GetParameters(idbManager.ProviderType, 5);
                arrParamDelete[0] = DBManagerFactory.GetParameterSQL("@RETURN_VALUE", System.Data.SqlDbType.Variant, 0, System.Data.ParameterDirection.ReturnValue, false, ((byte)(0)), ((byte)(0)), "", System.Data.DataRowVersion.Current, null);
                arrParamDelete[1] = DBManagerFactory.GetParameterSQL("@Original_Hocky", System.Data.SqlDbType.TinyInt, 0, System.Data.ParameterDirection.Input, false, ((byte)(0)), ((byte)(0)), "Hocky", System.Data.DataRowVersion.Original, null);
                arrParamDelete[2] = DBManagerFactory.GetParameterSQL("@Original_Mahs", System.Data.SqlDbType.NChar, 0, System.Data.ParameterDirection.Input, false, ((byte)(0)), ((byte)(0)), "Mahs", System.Data.DataRowVersion.Original, null);
                arrParamDelete[3] = DBManagerFactory.GetParameterSQL("@Original_Mamon", System.Data.SqlDbType.NChar, 0, System.Data.ParameterDirection.Input, false, ((byte)(0)), ((byte)(0)), "Mamon", System.Data.DataRowVersion.Original, null);
                arrParamDelete[4] = DBManagerFactory.GetParameterSQL("@Original_Makt", System.Data.SqlDbType.NChar, 0, System.Data.ParameterDirection.Input, false, ((byte)(0)), ((byte)(0)), "Makt", System.Data.DataRowVersion.Original, null);

                success = idbManager.UpdateDataSet(dtsCTKTRA, CommandType.StoredProcedure, "spCTKTRAThem", "spCTKTRAUpdate", "spCTKTRAXoa", arrParamInsert, arrParamUpdate, arrParamDelete);
            }
            catch (DatabaseExc e)
            {
                throw new BussinessExc(e.Message, e.ErrCodeUser, e.ErrCodeSQL);
            }
            catch (Exception e)
            {
                throw new BussinessExc("Bussiness. ICTKTRA.UpdateSP(DtsCTKTRA dtsCTKTRA)" + e.Message);
            }
            finally
            {
                idbManager.Dispose();
            }
            return success;
        }
        DataTable ICTKTRA.XemLOAIKTRA(byte hocky)
        {
            DataTable tbl = null;
            try
            {
                idbManager.CreateParameters(1);
                idbManager.AddParameters(0, "@Hocky", hocky, ParameterDirection.Input);
                idbManager.Open();
                tbl = idbManager.ExecuteDataSet(CommandType.StoredProcedure, "spLOAIKTRAXemTheoHocky").Tables[0];
            }
            catch (DatabaseExc e)
            {
                throw new BussinessExc(e.Message, e.ErrCodeUser, e.ErrCodeSQL);
            }
            catch (Exception e)
            {
                throw new BussinessExc("Bussiness. ICTKTRA.XemLOAIKTRA()" + e.Message);
            }
            finally
            {
                idbManager.Dispose();
            }
            return tbl;
        }
        DataTable ICTKTRA.XemDMLOP()
        {
            DataTable tbl = null;
            try
            {
                idbManager.Open();
                tbl = idbManager.ExecuteDataSet(CommandType.StoredProcedure, "spDMLOPXem").Tables[0];
            }
            catch (DatabaseExc e)
            {
                throw new BussinessExc(e.Message, e.ErrCodeUser, e.ErrCodeSQL);
            }
            catch (Exception e)
            {
                throw new BussinessExc("Bussiness. ICTKTRA.XemDMLOP()" + e.Message);
            }
            finally
            {
                idbManager.Dispose();
            }
            return tbl;
        }
    }
}